<?php

namespace App\Service;

use App\Jobs\Notification;
use App\Library\ChargeCache;
use App\Library\HttpClient;
use App\Library\Priv;
use Illuminate\Support\Facades\Log;
use App\Library\EncryptDecrypt;

class NotificationService
{
    use ChargeCache;
    use Priv;

    /**
     * 推送启动充电结果
     * @param $notice
     * @return void
     */
    public function notificationStartChargeResult($notice)
    {
        //修改缓存
        $this->setChargeStatusCache($notice['ConnectorID'], 2, $notice['StartChargeSeq']);
        //记录下这个开始充电时间开始计费 这里需要解密
        $priv = $this->getIvData($notice['OperatorID']);
        $decryptData = EncryptDecrypt::decrypt($notice['Data'], $priv['DataSecret'], $priv['DataSecretIV']);
        $decryptData = json_decode($decryptData, true);
        $decryptData['StartTime'] = date('Y-m-d H:i:s');
        $decryptData['OperatorID'] = $notice['OperatorID'];
        $this->setChargeCacle($notice['StartChargeSeq'], $decryptData);
        //请求三方结果
        $notice['action'] = 'notification_start_charge_result';
        (new HttpClient())->post($notice);
    }

    /**
     * 推送充电状态
     * @param $startChargeSeq
     * @return void
     */
    public function notificationEquipChargeStatus($startChargeSeq)
    {
        $res = $this->calcChageElc($startChargeSeq);
        //计算百分比
        $minute = abs((strtotime($res['StartTime']) - time()) % 86400 / 60);
        $sd = abs($minute * 60);
        $soc = bcdiv($sd, 10, 2);
        Log::info('秒数:'.$sd.'soc:'.$soc);
        $res['Soc'] = $soc;
        if ($soc >= 100) {
            //自动停止
            $data = ['StartChargeSeq' => $res['StartChargeSeq'], 'StartChargeSeqStat' => 3, 'SuccStat' => 0, 'FailReason' => '', 'OperatorID' => $res['OperatorID'], 'ConnectorID' => $res['ConnectorID']];
            $this->notificationStopChargeResult($data);
        }
        $notification = EncryptDecrypt::DataEn($res['OperatorID'], $res);
        $notification['action'] = 'notification_equip_charge_status';
        (new HttpClient())->post($notification);


    }

    /**
     * 推送停止充电结果
     * @param $notification
     * @return void
     */
    public function notificationStopChargeResult($notification)
    {

        $this->setChargeStatusCache($notification['ConnectorID'], 3, $notification['StartChargeSeq']);   //停止中
        $data = $this->calcChageElc($notification['StartChargeSeq']);
        $notification['action'] = 'notification_stop_charge_result';
        (new HttpClient())->post($notification);
        $data['StopReason'] = 100; //充电结束原因
        $this->notificationChargeOrderInfo($data);

    }

    /**
     * 推送充电订单信息
     * @param $data
     * @return void|false
     */
    public function notificationChargeOrderInfo($data)
    {
        if (empty($data)){
            return false;
        }
        $this->setChargeStatusCache($data['ConnectorID'], 4, $data['StartChargeSeq']);
        $notification = EncryptDecrypt::DataEn($data['OperatorID'], $data);
        $notification['action'] = 'notification_charge_order_info';
        (new HttpClient())->post($notification);
        //推送状态
        $this->notification_stationStatus($data['ConnectorID'], 1);
        //删除缓存
        $this->delCache($data['ConnectorID'], $data['StartChargeSeq']);
    }

    /**
     * 计费充电费用
     * @param $startChargeSeq
     * @return array
     */
    public function calcChageElc($startChargeSeq)
    {
        $data = [];
        try {
            $calc = $this->getChargeCacle($startChargeSeq);
            $result = json_decode($calc, true);
            if (empty($result)){
                return [];
            }
            $status = $this->getChargeStatusCache($result['ConnectorID']);
            if (empty($status)) {
                return [];
            }
            $status = json_decode($status, true);
            $second = abs((strtotime($result['StartTime']) - time()) % 86400);
            $data['StartChargeSeq'] = $startChargeSeq;
            $data['OperatorID'] = $result['OperatorID'];
            $data['StartChargeSeqStat'] = $second <= 20 ? 1 : $status['status'];
            $data['ConnectorID'] = $result['ConnectorID'];  //充电设备接口编码
            $data['StartTime'] = $result['StartTime'];  //开始充电时间
            $data['EndTime'] = date('Y-m-d H:i:s');  //结束充电时间
            $second = abs((strtotime($result['StartTime']) + 10 - strtotime($data['EndTime'])) % 86400 / 60);
            $elecMoney = bcmul(0.72, $second * 10, 2);   //电费
            $serviceMoney = bcmul(0.2, $elecMoney, 2);   //电费
            $data['TotalPower'] = bcdiv($elecMoney, 0.72, 2);  //累计充电量
            $data['TotalElecMoney'] = $elecMoney;  //总电费
            $data['TotalSeviceMoney'] = $serviceMoney;  //总服务费
            $totalMoney = bcadd($elecMoney, $serviceMoney, 2);
            $data['TotalMoney'] = $totalMoney;  //累计总金额
            $data['StopReason'] = "";   //停止原因
            $data['Vin'] = $this->getKey('vin');    //vin
        } catch (\Exception $exception) {
            Log::info("账单计算错误" . $exception->getMessage() . '-' . $exception->getLine() . '-' . $exception->getFile());
        }
        return $data;
    }

    /**
     * 推送设备状态变化
     * @param $ConnectorID
     * @param $Status
     * @return void
     */
    public function notification_stationStatus($ConnectorID, $Status = 1)
    {
        $data['ConnectorStatusInfo'] = ['ConnectorID' => $ConnectorID, 'Status' => $Status, 'ParkStatus' => 0, 'LockStatus' => 0];
        $notification = EncryptDecrypt::DataEn($this->myOperatorID, $data);
        $notification['action'] = 'notification_stationStatus';
        (new HttpClient())->post($notification);
    }
}
